<?php
declare (strict_types = 0);
namespace app\controller;

use think\facade\View;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use app\model\UserModel;
use app\Common;
use app\controller\Token;

Class Address{

    //常用数据表内容 及常用字典
    var $users =[];
    var $status=array('1'=>'正常','2'=>'停用');
    var $back = "<script>setTimeout(function(){history.back();},3000)</script>";

    public function __construct(){
        //准备一个类型数组
        $um=new UserModel();
        $this->users = $um->getAll();
   
    }

    //地址管理
    public function index(){
        $uid=Session::get('logonid');
        if(empty($uid)) return "请登录".$this->back;
        $list=Db::table('address')->where('customer_id',1)->paginate(15)->toArray();

        foreach ($list['data'] as &$row) {
            $row['status_name']= $this->status[$row['status']];
            $row['customer_id_name']=Db::table('customer')->where('id',$row['customer_id'])->value('name');
        }

        View::assign(['list'=>$list]);
        return View::fetch('index');
    }


    public function edit(){
        $uid=Session::get('logonid');
        if(!$uid) return header("location:/index.php/login/customer_login");

        $all = Request::param();
        //halt($_POST);

        if($_POST){
            if(empty($all['title'])) return "名称错误!".$this->back;
            if(empty($all['address'])) return "地址错误!".$this->back;
            if(empty($all['contact'])) return "联络人错误!".$this->back;
            if(empty($all['mobile'])) return "电话错误!".$this->back;

            $row = Db::table('address')->where('title',$all['title'])->where('id','<>',$all['id'])->find();
            if($row) return "名称已存在!".$this->back;

            //halt($_FILES);
            if($_FILES['file']['size']>0){
                $files =Common::upload();
                
                if($files['code']){
                    $all['imgs']=$files['msg'];
                }else{
                    return "<h1>".$files['msg']."</h1>".$this->back;
                }
            }

            Db::table('address')->update($all);
            return header('location:/address/index');
        }

        if(empty($all['id'])) return "id错误!".$this->back;
        $data=Db::table('address')->find($all['id']);
        View::assign(['data'=>$data]);

        return View::fetch('edit');

    }

    //场所新增
    public function add(){
        $uid=Session::get('logonid');
        if(!$uid) return header("location:/index.php/login/customer_login");

        $all = Request::param();

        if($_POST){
            if(!$all['title']) return '<h1>无效的名称!</h1>'.$this->back;;

            $row = Db::table('address')->where('title',$all['title'])->find();
            if(!empty($row)) return '<h1>场所名称['. $all['title']."]已存在！</h1>".$this->back; ;

            $all['customer_id']=$uid;
            $all['creater'] = 2;
            $all['create_time']  = date("Y-m-d H:i:s",time());

            //halt($_FILES);
            if($_FILES['file']['size']>0){
                $files =Common::upload();
                
                if($files['code']){
                    $all['imgs']=$files['msg'];
                }else{
                    return "<h1>".$files['msg']."</h1>".$this->back;
                }
            }
            
            $res = Db::table('address')->insert($all);
            if($res) {
                return "<h1>场所新增成功!</h1>".$this->back;
            }
            return "<h1>场所新增失败!</h1>".$this->back;
            
        }

        return View::fetch('add');
        
    }


    //地址管理
    public function  my_address(){
        $all = Request::param();
        if(empty($all['token'])){
            return json(['code'=>0,'msg'=>'需要token!']);
        }        
        $token =new Token();
        $arr = $token->check_token($all['token']);
        if(!$arr['code']){
            return $this->post_res($arr['msg']);
        }
        $me = $arr['customer'];

        $list_1=Db::table('address')->where('customer_id',$me['id'])->where('status',1)->select()->toArray();
        $list_2=Db::table('address')->where('customer_id',1)->where('status',1)->select()->toArray();

        return json(['code'=>1,'data'=>['my_add'=>$list_1,'serv_add'=>$list_2]]);
    }


    //新加、编辑地址
    public function  address_edit(){
        // $uid=Session::get('logonid');
        // if(!$uid) return header("location:/index.php/login/customer_login"); 

        $all = Request::param();
        if(empty($all['token'])){
            return json(['code'=>0,'msg'=>'需要token!']);
        }        
        $token =new Token();
        $arr = $token->check_token($all['token']);
        if(!$arr['code']){
            return $this->post_res($arr['msg']);
        }
        $me = $arr['customer'];

        $addr = array(
            'customer_id' => $me['id'],
            'address'=>$all['address'],
            'contact'=>$all['contact'],
            'mobile'=>$all['mobile'],
            //'lat'=>$all['lat'])?:'',
            //'lng'=>$all['lng']?:''
        );

        if(!empty($all['id'])) {
            $addr['id']=$all['id'];
            $res  = Db::table('address')->update($addr);
        }else{
            $add_e = Db::table('address')->where('address',trim($all['address']))->where('customer_id',$me['id'])->select()->toArray();
            if(count($add_e)){
                return json(['code'=>0,'msg'=>'地址已存在!']);
            }            
            $res  = Db::table('address')->insert($addr);
        }

        if($res) return json(['code'=>1,'msg'=>'地址已保存...']);
        return json(['code'=>0,'msg'=>'地址未修改！']);

    } 

    //删除地址
    public function  address_remove(){
        // $uid=Session::get('logonid');
        // if(!$uid) return header("location:/index.php/login/customer_login");  

        $all = Request::param();
        if(empty($all['id'])) return  json(['code'=>0,'msg'=>'没有地址！']);
        if(empty($all['token'])){
            return json(['code'=>0,'msg'=>'需要token!']);
        }

        $token =new Token();
        $arr = $token->check_token($all['token']);
        if(!$arr['code']){
            return $this->post_res($arr['msg']);
        }
        $me = $arr['customer'];
        
        $id = $all['id'];
        $res  = Db::table('address')->where('customer_id',$me['id'])->where('id',$id)->delete();
        if($res) return json(['code'=>1,'msg'=>'地址已删除...']);

        return json(['code'=>0,'msg'=>'失败!']);
    }    

    //祝福词
    public function wish(){
        $list = Db::table('wish')->where('status',1)->select()->toArray();
        return json(['code'=>1,'data'=>$list]);
    }


}